package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.View
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.activity.QuanXainOrganizationActivity
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.ManagerViewPageAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.RegisterStaticsActivityDelegate
import cc.vv.btpartyjob.partymanager.fragment.RegisterPartyStaticsFragment
import cc.vv.btpartyjob.partymanager.fragment.RegisterUnitStaticsFragment
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 报到统计
 * Created by Chen.qi on 2018/10/29.
 */
class RegisterStatisticsActivity : BaseActivityMVP<RegisterStaticsActivityDelegate>() {
    /**
     * ViewPager适配器
     */
    private var mAdapter: ManagerViewPageAdapter? = null

    /**
     * Fragment集合
     */
    private var mFragments: ArrayList<Fragment>? = null

    private var topNameUnit: String? = null

    private var topNameParty: String? = null

    private var currentPos: Int? = 0


    override fun initData() {
        viewDelegate?.setTopBarTitle("报到统计")
        mFragments = ArrayList()

        val userInfo = UserInfoManageUtil.getUserInfo()
        val userOrgList = userInfo?.userOrgList
        if (null != userOrgList) {
            for (i in userOrgList.indices) {
                if (userOrgList[i].type == 1) {
                    topNameUnit = userOrgList[i].name
                    topNameParty = userOrgList[i].name
                    break
                }
            }
        }
        viewDelegate?.setStreetName(topNameUnit)
        val registerUnit = RegisterUnitStaticsFragment()
        val registerParty = RegisterPartyStaticsFragment()
        mFragments!!.add(registerUnit)
        mFragments!!.add(registerParty)
        mAdapter = ManagerViewPageAdapter(supportFragmentManager, mFragments!!)
        viewDelegate?.setAdapterData(mAdapter!!)
        viewDelegate?.setRegisterYesSelect(true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }


            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //单位报到
            viewDelegate?.setRegisterYesSelect(true)
            viewDelegate?.setViewPagerPos(0)
        }, R.id.tv_ars_registerUnit)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //党员报到
            viewDelegate?.setRegisterYesSelect(false)
            viewDelegate?.setViewPagerPos(1)
        }, R.id.tv_ars_registerParty)

        viewDelegate?.bindViewPagerLisener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                currentPos = position

                if (position == 0) {
                    viewDelegate?.setStreetName(topNameUnit)
                    viewDelegate?.setRegisterYesSelect(true)
                } else {
                    viewDelegate?.setStreetName(topNameParty)
                    viewDelegate?.setRegisterYesSelect(false)
                }
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //组织选择
            val intent = Intent(this@RegisterStatisticsActivity, QuanXainOrganizationActivity::class.java)
            if (currentPos == 0) {
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 2)
            } else if (currentPos == 1) {
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 3)
            }
            startActivityForResult(intent, 100)
        }, R.id.ll_ars_street)
    }

    override fun getDelegateClass(): Class<RegisterStaticsActivityDelegate> {
        return RegisterStaticsActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            val orgId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            val type = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_TYPE)
            if (currentPos == 0) {
                topNameUnit = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME)
                viewDelegate?.setStreetName(topNameUnit)
            } else if (currentPos == 1) {
                topNameParty = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME)
                viewDelegate?.setStreetName(topNameParty)
            }

            try {
                if (currentPos == 0) {
                    val unitFrag = mFragments!![0] as RegisterUnitStaticsFragment
                    unitFrag.unitStaticsHttp(orgId, type)
                } else if (currentPos == 1) {
                    val partyFrag = mFragments!![1] as RegisterPartyStaticsFragment
                    partyFrag.partyStaticsHttp(orgId, type)
                }
            } catch (e: Exception) {

            }
        }
    }
}